<?php
    /**
	 * Created by Ailcc.com
     * 2021-11-14 12:00:00 沛霖主页
     * 
	 */
declare (strict_types = 1);

namespace app\index\controller;

use app\common\model\UrlModel;
use app\common\model\NewsModel;
use app\common\model\NewsCategoryModel;
use app\common\model\MessagesModel;
use think\facade\Cookie;
use think\facade\Lang;
use think\facade\View;

class Newsimg extends Common
{
	protected function initialize(){
		parent::initialize(); // TODO: Change the autogenerated stub
	}

	public function index(){
        
    }
     
    public function news_image(){
        $id = input('id');   
        $action = input('action');
		if (!is_numeric($id)) {     //判断ID是否为字符或数字
            $id_domain = $id ;      //如为字符则查询查询字符所属文章ID
            if ($id_domain) {       //判断个性域名
                $item = NewsModel::where('id_domain',$id_domain)->find();
                if ($item['id']) {
                    $id = $item['id'] ;
                }
            }
        }
        $info = NewsModel::info($id);
        $html ='';
        $peilin_home = 'https://ailcc.com/static/image/peilin_home.png';    //海报logo图
        if(empty($info)){
            echo('无效的文章ID:'.$id.'');
			exit;
        }else{
            $str = mb_strimwidth($info['description'], 0, 80) . '...'; //限制文章描述长度为80字符
            echo '
    <div class="post-poster">
        <div class="post-poster-main" id="post-poster-main-'.$id.'">
            <div class="cover">
                <img src="'.$info['thumb'].'" alt="">
            </div>
            <div class="content">
                <p class="title mt20 fs16">'.$info['title'].'</p>
                <p class="excerpt fs14 mt20 c-sub">'.$str.'</p>
                <div class="info mt20">
                    <img class="qrcode" src="'.$info['news_image'].'" alt="'.$info['title'].'">
                    <img class="logo" src="'.$peilin_home.'" alt="logo">
                </div>
                <p class="tip c-sub fs12 mt20 p-flex-center"><i class="fa-solid fa-qrcode"></i>&nbsp;长按识别二维码查看文章内容</p>
            </div>
        </div>
    </div>
    <script>
        $(function () {
            html2canvas(document.querySelector("#post-poster-main-'.$id.'"), {
                allowTaint: true,
                useCORS: true
            }).then(canvas => {
                const el = $("#post-poster-main-'.$id.'");
                el.show();
                el.html("<img class=\'result\' src=\'" + canvas.toDataURL("image/png") + "\' alt=\''.$info['title'].'\'>");
            }).catch(err => {
                console.error(err)
                window.Puock.toast("生成海报失败，请到Console查看错误信息", TYPE_DANGER);
            });
        })
    </script>';
        }
    }

}
